package com.atsmart.app.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.provider.Settings;
import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
//import com.atsmart.app.activity.ClientListActivity;
//import com.atsmart.app.activity.MessageListActivity;
//import com.atsmart.app.activity.OtherListActivity;
//import com.atsmart.app.activity.ProductListActivity;
//import com.atsmart.app.activity.ReportListActivity;
//import com.atsmart.app.activity.TaskListActivity;

public class DialogUtil {
	private static DialogUtil dialogUtil = null;

	static {
		if (dialogUtil == null) {
			dialogUtil = new DialogUtil();
		}
	}

	public static void dialogWithErrorMsg(Context context, String errorString) {
		// AlertDialog.Builder builder = new Builder(context);
		// builder.setMessage(errorString)
		// .setTitle(context.getResources().getString(R.string.app_name))
		// .setPositiveButton(
		// context.getResources().getString(R.string.confirm),
		// new OnClickListener() {
		// @Override
		// public void onClick(DialogInterface dialog,
		// int which) {
		// dialog.dismiss();
		// }
		// }).show();
	}

	public static ProgressDialog getProgressDialog(Context context,
			String messageString) {
		return getProgressDialog(context, ProgressDialog.STYLE_SPINNER,
				messageString);
	}

	public static Dialog getLoadingDialog(Context context) {
		Dialog dialog = new Dialog(context,
				android.R.style.Theme_Translucent_NoTitleBar);
		dialog.setContentView(R.layout.loading_fullscreen);
		RelativeLayout view = (RelativeLayout) dialog.findViewById(R.id.dialog);
		view.setBackgroundResource(R.color.black);
		view.getBackground().setAlpha(120);
		return dialog;
	}

	public static Dialog getLoadingDialog(Context context, String mess) {
		Dialog dialog = new Dialog(context,
				android.R.style.Theme_Translucent_NoTitleBar);
		dialog.setContentView(R.layout.loading_fullscreen);
		RelativeLayout view = (RelativeLayout) dialog.findViewById(R.id.dialog);
		TextView message = (TextView) dialog.findViewById(R.id.message);
		message.setText(mess);
		view.setBackgroundResource(R.color.black);
		view.getBackground().setAlpha(120);
		return dialog;
	}

	public static ProgressDialog getProgressDialog(Context context, int style,
			String messageString) {
		ProgressDialog dialog = new ProgressDialog(context);
		dialog.setProgressStyle(style);
		dialog.setIndeterminate(true);
		dialog.setCancelable(true);
		dialog.setMessage(messageString);
		return dialog;
	}

	public static Dialog getLoadingDialogFullscreen(Context context) {
		Dialog dialog = new Dialog(context,
				android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		// dialog.setContentView(R.layout.loading_fullscreen);
		// dialog.setCancelable(false);
		// LinearLayout view = (LinearLayout) dialog.findViewById(R.id.dialog);
		// view.setGravity(Gravity.CENTER | Gravity.TOP);
		// view.setPadding(0, 200, 0, 0);
		return dialog;
	}

	public static AlertDialog getAlertDialog(Activity currentActivity,
			String title, String messageString, String positiveButtonText,
			DialogInterface.OnClickListener positiveButtonListener,
			CharSequence negativeButton,
			DialogInterface.OnClickListener negativeButtonListener) {
		return getAlertDialog(currentActivity, title, messageString,
				positiveButtonText, positiveButtonListener, negativeButton,
				negativeButtonListener, true, true);
	}

	private static AlertDialog getAlertDialog(Activity currentActivity,
			String title, String messageString, String positiveButtonText,
			DialogInterface.OnClickListener positiveButtonListener,
			CharSequence negativeButton,
			DialogInterface.OnClickListener negativeButtonListener,
			boolean isUsePositiveButton, boolean isUseNegativeButton) {
		AlertDialog.Builder builder = new AlertDialog.Builder(currentActivity);
		if (TextUtils.isEmpty(title)) {
			builder.setTitle(R.string.app_name);
		} else {
			builder.setTitle(title);
		}
		if (!TextUtils.isEmpty(messageString)) {
			builder.setMessage(messageString).setCancelable(false);
		}
		if (isUsePositiveButton) {
			builder.setPositiveButton(positiveButtonText,
					positiveButtonListener);
		}
		if (isUseNegativeButton) {
			builder.setNegativeButton(negativeButton, negativeButtonListener);
		}
		return builder.create();
	}

	public static AlertDialog getExitAppDialog(final Activity activity) {
		AlertDialog dialog = new AlertDialog.Builder(activity)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle(R.string.alert_title)
				.setMessage(R.string.confirm_exit)
				.setPositiveButton(R.string.yes,
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								PrefsUtils.setIsNoti("1");
								BizApp.getInstance().setExited(true);
								affirmExitApp(activity, dialog);
							}

						}).setNegativeButton(R.string.no, null).create();
		return dialog;
	}

	private static void affirmExitApp(final Activity activity,
			DialogInterface dialog) {
		dialog.dismiss();

//		if (((activity.getClass() == TaskListActivity.class
//				|| activity.getClass() == ReportListActivity.class || activity
//				.getClass() == ClientListActivity.class) || activity.getClass() == MessageListActivity.class)
//				|| (activity.getClass() == OtherListActivity.class)
//				|| activity.getClass() == ProductListActivity.class) {
//			activity.finish();
//		} else {
//			Intent intent = new Intent();
//			intent.setClass(activity, TaskListActivity.class);
//			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//			activity.finish();
//			activity.startActivity(intent);
//		}
	}

	public static void showLocationSetting(final Context context) {
		new AlertDialog.Builder(context)
				.setTitle(R.string.alert_title)
				.setMessage(R.string.setting_network_tips)
				.setPositiveButton(R.string.yes,
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								context.startActivity(new Intent(
										Settings.ACTION_LOCATION_SOURCE_SETTINGS));
							}
						}).setNegativeButton(R.string.no, null).create().show();
	}

}
